function notify (url, msg)
    os.execute("notify-send '"..url.."' "..msg)
end
    
function handle_url_simple_copy (url)
    -- simply copies the url
    -- requires package xclip
    
    local url = string.gsub(url,"\n.+$", "")
    os.execute ("echo -n '"..url.."' | xclip -i -selection clipboard")
    notify (url, "copied")
end
    
function handle_url_simple_open (url)
    -- simply opens the url
    -- requires package xdg-utils
    
    local url = string.gsub(url,"\n.+$", "")
    os.execute ("xdg-open '"..url.."'")
    notify (url, "opened")
end

function handle_url_zenity (url)
    -- pops up a dialog to allow choice of copying or opening the url
    -- requires package zenity and the termit-zenity-dialog script
    
    local url = string.gsub(url,"\n.+$", "")
    os.execute("termit-zenity-dialog '"..url.."' &") -- make it async otherwise this termit waits
end
    
function handle_url_lgi (url)
    -- pops up a dialog to allow choice of copying or opening the url
    -- also allows editing of the url before copying/opening
    -- requires package lua-lgi and the termit-lgi-dialog script
    
    local url = string.gsub(url,"\n.+$", "")
    os.execute("termit-lgi-dialog '"..url.."' &") -- make it async otherwise this termit waits
end

defaults = {}
defaults.windowTitle = 'Termit'
defaults.tabName = 'Terminal'
defaults.encoding = 'UTF-8'
defaults.wordChars = '+-AA-Za-z0-9,./?%&#:_~'
defaults.scrollbackLines = 4096
defaults.font = 'DejaVu Sans Mono 10'
defaults.geometry = '80x24'
defaults.hideSingleTab = false
defaults.showScrollbar = false
defaults.showBorder = false
defaults.fillTabbar = false
defaults.hideMenubar = true
defaults.hideTabbar = true
defaults.allowChangingTitle = true
defaults.visibleBell = false
defaults.audibleBell = false
defaults.urgencyOnBell = false
defaults.matches = {['http[^ \'\"]+'] = handle_url_lgi } -- choose one of the above

setOptions(defaults)

function get_primary()
    os.execute ("type xvkbd && xvkbd -text '\\m2' || xdotool click 2")
end

function changeTabFontSize(delta)
    tab = tabs[currentTabIndex()]
    setTabFont(string.sub(tab.font, 1, string.find(tab.font, '[0-9.]+$') - 1)..(tab.fontSize + delta))
end

bindKey('Ctrl-t', nil) -- needed for bash
bindKey('CtrlShift-t', openTab)
bindKey('CtrlShift-w', closeTab)
bindKey('CtrlShift-Tab', prevTab)
bindKey('Ctrl-Tab', nextTab)
bindKey('CtrlShift-f', toggleMenubar)
bindKey('CtrlShift-g', toggleTabbar)
bindKey('CtrlShift-v', paste)
bindKey('CtrlShift-c', copy)
bindKey('CtrlShift-s', saveSessionDlg)
bindKey('CtrlShift-l', loadSessionDlg)
bindKey('Shift-Insert', function() get_primary() end)
bindKey('Ctrl-F1', function () activateTab(1) end)
bindKey('Ctrl-F2', function () activateTab(2) end)
bindKey('Ctrl-F3', function () activateTab(3) end)
bindKey('Ctrl-F4', function () activateTab(4) end)
bindKey('Ctrl-F5', function () activateTab(5) end)
bindKey('Ctrl-F6', function () activateTab(6) end)
bindKey('Ctrl-F7', function () activateTab(7) end)
bindKey('Ctrl-F8', function () activateTab(8) end)
bindKey('Ctrl-F9', function () activateTab(9) end)

setKbPolicy('keycode')

